<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="父级序号" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父级序号"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="seq">
      <el-input v-model="dataForm.seq" placeholder="排序"></el-input>
    </el-form-item>
    <el-form-item label="菜单名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="菜单名称"></el-input>
    </el-form-item>
    <el-form-item label="提示信息" prop="tip">
      <el-input v-model="dataForm.tip" placeholder="提示信息"></el-input>
    </el-form-item>
    <el-form-item label="菜单说明" prop="descn">
      <el-input v-model="dataForm.descn" placeholder="菜单说明"></el-input>
    </el-form-item>
    <el-form-item label="图片地址" prop="imageUrl">
      <el-input v-model="dataForm.imageUrl" placeholder="图片地址"></el-input>
    </el-form-item>
    <el-form-item label="链接地址" prop="linkUrl">
      <el-input v-model="dataForm.linkUrl" placeholder="链接地址"></el-input>
    </el-form-item>
    <el-form-item label="目标窗口" prop="target">
      <el-input v-model="dataForm.target" placeholder="目标窗口"></el-input>
    </el-form-item>
    <el-form-item label="是否可用" prop="status">
      <el-input v-model="dataForm.status" placeholder="是否可用"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          parentId: '',
          seq: '',
          name: '',
          tip: '',
          descn: '',
          imageUrl: '',
          linkUrl: '',
          target: '',
          status: ''
        },
        dataRule: {
          parentId: [
            { required: true, message: '父级序号不能为空', trigger: 'blur' }
          ],
          seq: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '菜单名称不能为空', trigger: 'blur' }
          ],
          tip: [
            { required: true, message: '提示信息不能为空', trigger: 'blur' }
          ],
          descn: [
            { required: true, message: '菜单说明不能为空', trigger: 'blur' }
          ],
          imageUrl: [
            { required: true, message: '图片地址不能为空', trigger: 'blur' }
          ],
          linkUrl: [
            { required: true, message: '链接地址不能为空', trigger: 'blur' }
          ],
          target: [
            { required: true, message: '目标窗口不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '是否可用不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/sysmenus/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then((data) => {
              if (data && data.code === 200) {
                this.dataForm.parentId = data.sysMenus.parentId
                this.dataForm.seq = data.sysMenus.seq
                this.dataForm.name = data.sysMenus.name
                this.dataForm.tip = data.sysMenus.tip
                this.dataForm.descn = data.sysMenus.descn
                this.dataForm.imageUrl = data.sysMenus.imageUrl
                this.dataForm.linkUrl = data.sysMenus.linkUrl
                this.dataForm.target = data.sysMenus.target
                this.dataForm.status = data.sysMenus.status
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/purchase/sysmenus/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'parentId': this.dataForm.parentId,
                'seq': this.dataForm.seq,
                'name': this.dataForm.name,
                'tip': this.dataForm.tip,
                'descn': this.dataForm.descn,
                'imageUrl': this.dataForm.imageUrl,
                'linkUrl': this.dataForm.linkUrl,
                'target': this.dataForm.target,
                'status': this.dataForm.status
              })
            }).then((data) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
